<?php

//require_once "html/extended/spAddToListBase.php";

class spAddToListCombo extends spAddToListBase
{
	public function __construct( $data=null )
	{
		parent::__construct( $data );
		parent::setClass( "spAddToListBase spAddToListCombo_new" );

		/*
		// this is where we insert already predefined items
		if ( isset($data["items"]) && count($data["items"]) )
		{
			$insert_here = $this->find("insert_here");

			foreach ( $data["items"] as $selected_value )
			{
				$insert_here->addChild( $this->createCloneableContainer( 
					array( "selected_value"=>$selected_value ) 
				));
			}
		}
		*/
	}
	
	protected function createInsertHtml( $param=array() )
	{
		$data = parent::getData();
		
		$selected_value = isset($param["selected_value"])?$param["selected_value"]:"";

		// FIXME: remove wordpres code and find a way to create the path to the image
		$img = get_option('home').'/wp-content/plugins/gym/sigmaphp/html/extended/dragbar_gray.png';
		//$img = "dragbar_gray.png";

		// image
		$i = new spImage(array("src"=>$img));
		$i->addAttr( "align", "top" );
		
		// combobox
		$combobox = new spCombobox( 
			array( 
				"name"			=>$data["name"]."[]", 
				"list"			=>$data["list"],
				"selected_value"=>$selected_value 
		));		
		
		$table = new spTableExt;
		$table->addRowChilds( array(
			$i,
			$combobox
		));
		
		return $table;
	}	
	
	public function setElementDataString( $s )
	{
		parent::setElementDataString( $s );
	
		$ar = explode( ",", $s );
		
		if ( count($ar) )
		{
			$insert_here = $this->find("insert_here");
		
			foreach ( $ar as $a )
			{
				$insert_here->addChild( $this->createCloneableContainer( 
					array( "selected_value"=>$a ) 
				));
			}
		}
	}	
	
	public function updateFromRequest()
	{
		parent::updateFromRequest();

		// convience variables
		$data = parent::getData();
		$r = $_REQUEST;	
		
		$s = implode( ",", $r[ $data["name"] ] );
		
		$this->setElementDataString( $s );
	}	
}
		

?>